package com.fyyg.common.lottery;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.CronExpression;

/**
 * @author hang.ouyang
 * @date 2016-04-27 13:34
 */
public class LotteryUtil {

    private final static Logger logger = LogManager.getLogger(LotteryUtil.class);

    private  static  SimpleDateFormat sf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //10:00-21:50
    private static String cronExpressionS = "0 0/10 10-21 * * ?";
    //22:00-01:55
    private static String cronExpressionE = "0 0/5 22-01 * * ?";

    /**
     * 开奖时间
     * @param fromTimstamp  购物结束时间戳
     * @return
     */
    public static String lotteryOpenTime(Long fromTimstamp){
        Calendar calendar= Calendar.getInstance();
        fromTimstamp = fromTimstamp.toString().length() < 13 ? fromTimstamp*1000 : fromTimstamp;
        calendar.setTime(new Timestamp(fromTimstamp));
        String cronExpression = getCronExpression(calendar);
        Date fDate = new Date(fromTimstamp);
        Date date = nextTimes_(cronExpression,fDate);
        calendar.setTime(date);
//        if(calendar.get(Calendar.HOUR_OF_DAY) == 2 && calendar.get(Calendar.MINUTE) == 0){
//            date = nextTimes_(cronExpression,date);
//        }
        long diff = date.getTime() - fDate.getTime();
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        if(min >= 3 || hour >= 1 || day >= 1){
            logger.info("next ssc open time "+sf.format(date));
            System.out.println(sf.format(date));
            return (new Date(date.getTime()+3*nm).getTime()/1000L)+"";
        }else{
            return lotteryOpenTime(date.getTime());
        }

    }

    public static String getLotterOpentimeBySscTime(Long sscTimeStamp){
        Calendar calendar= Calendar.getInstance();
        sscTimeStamp = sscTimeStamp.toString().length() < 13 ? sscTimeStamp*1000 : sscTimeStamp;
        calendar.setTime(new Date(sscTimeStamp));
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        if(hour >= 10 && hour <= 21){
            minute = minute/10*10;
        }else{
            minute = minute/5*5;
        }
        minute += 3;
        calendar.set(year,month,day,hour,minute,0);
        System.out.println(sf.format(calendar.getTime()));
        return (calendar.getTime().getTime())/1000L+"";
    }


    private static String getCronExpression(Calendar calendar){
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        if(hour >= 2 && hour <= 21){
            if(hour == 21 && minute >= 48)
                return cronExpressionE;
            return cronExpressionS;
        }else {
            if(hour == 1 && minute >= 53)
                return cronExpressionS;
            return cronExpressionE;
        }
    }

    private static Date nextTimes_(String expression,Date fromDate){
        try {
            CronExpression cronExpression = new CronExpression(expression);
            Date date = cronExpression.getNextInvalidTimeAfter(fromDate);
            return  cronExpression.getNextValidTimeAfter(date);
        } catch (ParseException e) {
            logger.error(e.getLocalizedMessage());
        }
        return null;
    }
    private static String nextTimes(String expression,Date fromDate){
        try {
            CronExpression cronExpression = new CronExpression(expression);
            Date date = cronExpression.getNextInvalidTimeAfter(fromDate);
            return  sf.format(cronExpression.getNextValidTimeAfter(date));
        } catch (ParseException e) {
            logger.error(e.getLocalizedMessage());
        }
        return null;
    }

 

	public static void main(String[] args) {
		System.out.println(LotteryUtil.lotteryOpenTime(1468198793L));
    }
}
